package com.love.marriage.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 公众号微信支付配置
 */
@Slf4j
@Configuration
public class WxGzhPayConfig {

    @Autowired
    private WeChatConfig weChatConfig;

    @Autowired
    private WeChatGzhConfig weChatGzhConfig;

    @Value("${wechat.pay.sandbox.enabled:false}")
    private boolean sandboxEnabled;
    
    @Value("${wechat.pay.sandbox.mchId:}")
    private String sandboxMchId;
    
    @Value("${wechat.pay.sandbox.apiKey:}")
    private String sandboxApiKey;

    /**
     * 公众号微信支付服务（API V2版本）
     */
    @Bean("gzhPayService")
    public WxPayService gzhPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(weChatGzhConfig.getAppId()); // 使用公众号AppId
        
        // 沙箱环境配置
        if (sandboxEnabled) {
            log.info("配置微信支付V2沙箱环境");
            payConfig.setUseSandboxEnv(true);
            
            // 沙箱环境使用专用商户号和API密钥
            String mchId = !sandboxMchId.isEmpty() ? sandboxMchId : weChatConfig.getMchid();
            String apiKey = !sandboxApiKey.isEmpty() ? sandboxApiKey : weChatConfig.getApiV2();
            
            payConfig.setMchId(mchId);
            payConfig.setMchKey(apiKey);
            payConfig.setKeyPath(weChatConfig.getKeyPath());
            payConfig.setSignType("MD5");
            // V2版本不使用V3相关配置
            
            log.info("V2沙箱环境配置: AppId={}, MchId={}, ApiKey={}****", 
                    weChatGzhConfig.getAppId(), mchId, 
                    apiKey.length() > 4 ? apiKey.substring(0, 4) : "****");
        } else {
            log.info("配置微信支付V2生产环境");
            payConfig.setUseSandboxEnv(false);
            payConfig.setMchId(weChatConfig.getMchid());
            payConfig.setMchKey(weChatConfig.getApiV2());
            payConfig.setKeyPath(weChatConfig.getKeyPath());
            payConfig.setSignType("MD5");
            // V2版本不需要V3相关配置
        }
        
        payConfig.setNotifyUrl(weChatConfig.getNotifyUrl());
        
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    /**
     * 测试沙箱配置是否正确
     * 可以通过调用此方法来验证沙箱环境配置
     */
    public void testSandboxConfig() {
        if (sandboxEnabled) {
            log.info("=== 沙箱环境配置测试 ===");
            log.info("沙箱环境已启用: {}", sandboxEnabled);
            log.info("公众号AppId: {}", weChatGzhConfig.getAppId());
            
            String mchId = !sandboxMchId.isEmpty() ? sandboxMchId : weChatConfig.getMchid();
            String apiKey = !sandboxApiKey.isEmpty() ? sandboxApiKey : weChatConfig.getApiV2();
            
            log.info("商户号: {}", mchId);
            log.info("API密钥: {}****", apiKey.length() > 4 ? apiKey.substring(0, 4) : "****");
            log.info("证书路径: {}", weChatConfig.getKeyPath());
            log.info("=========================");
        } else {
            log.info("当前使用生产环境，未启用沙箱环境");
        }
    }
} 